﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;

namespace PeisongWeb
{
    public partial class buyer_reportPayableList : buyerPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("CB", 1))
                {
                    showNotPayable();
                    rptBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "buyer_index.html");
                    return;
                }
            }
        }
        private void showNotPayable()
        {
            DataRow dr = logic.paymentHistory.listWithExtend(logic.customer.companyId).Rows[0];
            int pmtNum = Convert.ToInt32(dr["pmtNum"]);
            if (pmtNum != 0)
            {
                string money = string.Format("{0:N}", dr["sumAmount"]);
                sp_Num.InnerText = dr["pmtNum"].ToString();
                sp_total.InnerText = sp_total2.InnerText = money;
                sp_Day.InnerText = dr["syDay"].ToString();
                if (Convert.ToInt32(dr["syDay"]) <= 0)
                {
                    li_day.Visible = false;
                    li_chao.Visible = true;
                }
            }
            else
            {
                dv_payable.Visible = false;
            }
        }
        void rptBind()
        {
            int buyerId = logic.customer.getCompanyId();
            StringBuilder where = new StringBuilder();
            where.Append("type='1' and companyId=" + buyerId);
            if (ext_StartInputDt.SelectedDate != DateTime.MinValue && ext_StartInputDt.Value.ToString() != "")
                where.AppendFormat(" and datediff(day,[startDt],'{0}')<=0", ext_StartInputDt.Value.ToString());
            if (ext_EndInputDt.SelectedDate != DateTime.MinValue && ext_EndInputDt.Value.ToString() != "")
                where.AppendFormat(" and datediff(day,[endDt],'{0}')<=0", ext_EndInputDt.Value.ToString());
            if(ddl_isPay.SelectedValue!="")
                where.AppendFormat(" and status ='{0}' ", ddl_isPay.SelectedValue.ToString());
            DataSet ds = logic.paymentHistory.select(where.ToString(), "payId desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex); ;
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();

            showCollect();
        }       
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }

        protected void btn_select_Click(object sender, EventArgs e)
        {
            rptBind();
        }

        private void showCollect()
        {
            int companyId = logic.customer.getCompanyId();
            DateTime? startDt = null;
            DateTime? endDt = null;
            if (ext_StartInputDt.SelectedDate != DateTime.MinValue) startDt = ext_StartInputDt.SelectedDate;
            if (ext_EndInputDt.SelectedDate != DateTime.MinValue) endDt = ext_EndInputDt.SelectedDate;

            DataRow dr = logic.paymentHistory.listWithCollect(companyId, startDt, endDt, "1").Rows[0];
            lit_orderNum.Text = string.Format("{0:N}", dr["orderNum"]);
            lit_payMoney.Text = string.Format("{0:N}",dr["payMoney"]);
            lit_notPayMoney.Text = string.Format("{0:N}", dr["notPayMoney"]);
            lit_total.Text = string.Format("{0:N}", dr["orderAmount"]);
        }
        protected void lbn_searchNotPay_Click(object sender, EventArgs e)
        {
            ddl_isPay.SelectedValue = "1";
            showNotPayable();
            rptBind();
        }
    }
}
